const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');

// 确保miniprogram_npm目录存在
const npmDistDir = path.join(__dirname, 'miniprogram', 'miniprogram_npm');
if (!fs.existsSync(npmDistDir)) {
  fs.mkdirSync(npmDistDir, { recursive: true });
}

// 复制TDesign组件库到miniprogram_npm目录
const tdSourceDir = path.join(__dirname, 'node_modules', 'tdesign-miniprogram');
const tdDestDir = path.join(npmDistDir, 'tdesign-miniprogram');

// 递归复制函数
function copyFolderSync(from, to) {
  if (!fs.existsSync(to)) {
    fs.mkdirSync(to, { recursive: true });
  }

  fs.readdirSync(from).forEach(element => {
    const fromPath = path.join(from, element);
    const toPath = path.join(to, element);
    
    if (fs.lstatSync(fromPath).isFile()) {
      fs.copyFileSync(fromPath, toPath);
    } else {
      copyFolderSync(fromPath, toPath);
    }
  });
}

console.log('开始复制TDesign组件库...');
copyFolderSync(tdSourceDir, tdDestDir);
console.log('TDesign组件库复制完成！');

console.log('npm构建完成！'); 